<template>
	<view>
		<!-- 搜索区域 -->
		<view class="search-box">
			<my-search @click="goToSearch"></my-search>
		</view>

		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, index) in swiperList" :key="index">
				<navigator class="swiper-item" :url="`/subpackages/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src" class="swiper-pic"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, index) in navList" :key="index">
				<image :src="item.image_src" class="nav-img" @click="onSkipCateTabBar(item)"></image>
			</view>
		</view>

		<!-- 楼层主体区域 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 楼层图片区域 -->
				<view class="floor-img-box">
					<!-- 左边图片区域 -->
					<navigator class="floor-left-img" :url="item.product_list[0].navigator_url">
						<image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
							mode="widthFix"></image>
					</navigator>
					<!-- 右边图片区域 -->
					<view class="floor-right-img">
						<navigator class="floor-right-img-item" v-for="(item2, index2) in item.product_list" :key="index2"
							v-if="index2 !== 0" :url="item2.navigator_url">
							<image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex'
	// 导入自己封装的 mixin 模块
	import tabBarIndex from '@/mixins/tabbar-badge.js'
	export default {
		// 将 tabBarIndex 混入到当前的页面中进行使用
		mixins: [tabBarIndex],
		data() {
			return {
				swiperList: [], // 轮播图数据
				navList: [], // 导航数据
				floorList: [], // 楼层的数据列表
			};
		},
		onLoad() {
			// 获取轮播图数据
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			// 获取轮播图数据Api
			async getSwiperList() {
				const res = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 数据请求失败
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 请求数据成功
				this.swiperList = res.data.message
			},

			// 获取导航数据Api
			async getNavList() {
				const res = await uni.$http.get('/api/public/v1/home/catitems')
				// 请求失败
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 请求成功
				this.navList = res.data.message
			},

			// 获取楼层数据Api
			async getFloorList() {
				const res = await uni.$http.get('/api/public/v1/home/floordata')
				// 请求失败
				if (res.data.meta.status !== 200) return uni.$showMsg()
				// 请求成功
				// 修改floorList.product_list中的navigator_url路径
				res.data.message.forEach(item => {
					item.product_list.forEach(item2 => {
						// 将pages全部转换为subpackages/goods_list
						item2.navigator_url = item2.navigator_url.replace('pages', 'subpackages/goods_list')
					})
				})
				this.floorList = res.data.message
			},

			// 点击跳转到分类
			onSkipCateTabBar(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},

			// 跳转到搜索页面
			goToSearch() {
				uni.navigateTo({
					url: "/subpackages/search/search"
				})
			},
		},
	}
</script>

<style lang="scss">
	swiper {
		height: 330rpx;
	}

	.swiper-item,
	.swiper-pic {
		width: 100%;
		height: 100%;
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;
	}

	.nav-img {
		width: 128rpx;
		height: 140rpx;
	}

	.floor-title {
		width: 100%;
		height: 60rpx;
	}

	.floor-img-box {
		display: flex;
		padding-left: 10rpx;
	}

	.floor-right-img {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}

	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
</style>